package filter;

import java.util.*;
import java.io.*;

/////////////////////////////////////
//Creates a .txt file that is a    //
//list of counties associated with //
//given zip codes.				   //
//								   //
//The data must be formatted in a  //
//specific way, with an "eof"      //
//appended to the end.			   //
//								   //
//Author: Sam Burich			   //
//Date: 10 October 2011			   //
/////////////////////////////////////
public class Main {

	//HashMap to associate zip codes with the county in which they are primarily located
	public static HashMap<Integer,String> countyHash = new HashMap<Integer, String>();
	
	//main()
	public static void main(String args[])
	{
		//Initialize the hash table
		initHash();
		try
		{
			//Begin reading from the file of zip code data
			BufferedReader reader = new BufferedReader(new FileReader(new File("2009TopFiveStates.csv")));
			//Crop the first line, because the data has headers
			reader.readLine();
			//Create a new file and a way to write to it
			PrintWriter writer = new PrintWriter(new BufferedWriter(new FileWriter(new File("output2009.txt"))));
			//String to hold lines from data file
			String s = "temp";
			//Loop forever, until "eof" is reached
			while(true)
			{
				//Read a line of data
				s = reader.readLine();
				//Break if "eof" - end of file
				if(s.equals("eof")) break;
				//Trim all but the zip code
				s = s.substring(9);
				//Turn the zip code into an integer
				int z = Integer.parseInt(s);
				//Output the county matched to the given zip code
				writer.println(countyFromZipCode(z));
				System.out.println(countyFromZipCode(z));
			}
			//Due to errors encountered while printing,
			//print the word "Egg" ten thousand times.
			//This allows the BufferedWriter to finish
			//printing the counties.
			for(int i = 0; i < 10000; i++)
			{
				writer.println("Egg");
				System.out.println("Egg");
			}
			//Close the files
			reader.close();
			writer.close();
		}
		catch(Exception e)
		{
			//Catch all exceptions and exit
			System.err.println("Fail");
		}
		return;
	}

	//initHash()
	private static void initHash()
	{
		try
		{
			//Open the data that associates zip codes with counties
			BufferedReader buff = new BufferedReader(new FileReader(new File("zipcounty.txt")));
			//Loop until "eof" is reached
			while(true)
			{
				//Read a line of data
				String line = buff.readLine();
				//Break if "eof" - end of file
				if(line.equals("eof")) break;
				//Separate the necessary data
				int zipcode = Integer.parseInt(line.substring(0,5));
				String county = line.substring(6);
				//Populate the HashMap
				countyHash.put(zipcode, county);
			}
		}
		catch(Exception e)
		{
			//Catch all exceptions and exit
			System.err.println("Init Fail");
		}
		//Fill all unmatched entries with dummy values
		for(int i = 0; i < 100000; i++)
		{
			if(countyHash.get(i) == null)
			{
				countyHash.put(i, "Unknown*XX");
			}
		}
		return;
	}
	
	//countyFromZipCode()
	//Getter method
	private static String countyFromZipCode(int zipcode)
	{
		return countyHash.get(zipcode);
	}
}